package me.atbar.moonlib.util;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @author Administrator
 */
public class FileUtils {

    private FileUtils() {
    }

    public static File getFileInstance(String path) {
        return new File(path);
    }

    public static boolean deleteFile(String path) {
        File file = new File(path);
        file.delete();
        return true;
    }

    public static boolean saveFile(String path) {
        File file = new File(path);
        try {
            if(!file.exists()) {
                file.createNewFile();
            }
            return true;
        } catch (Exception error) {
            error.getMessage();
            return false;
        }
    }

    public static void createFile(String path) {
        File file = new File(path);
        if(!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        try {
            if(!file.exists()) {
                file.createNewFile();
            }
        } catch (Exception e) {
            e.getMessage();
        }
    }

    public boolean reset(String path) throws IOException {
        File config = getFileInstance(path);
        FileWriter fileWriter = new FileWriter(config);
        try {
            createFile(path);
            fileWriter.write("");
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        } finally {
            fileWriter.flush();
            fileWriter.close();
        }
    }

}
